package com.findviewbydream.evil.helloyouth;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;

import com.findviewbydream.evil.helloyouth.Utils.Utils;


/********************************************
 * 工程名：HelloYouth
 * 包名：com.findviewbydream.evil.helloyouth
 * 时间：16-6-28 下午8:08
 * 作者：白雨濃 evil
 * 邮箱：evilbai@foxmail.com
 * 内容：此基础Activity包含toolbar
 ********************************************/
public abstract class BaseActivity extends AppCompatActivity
{
    private String title;

    protected Toolbar toolbar;

    public abstract void initContentView(Bundle savedInstanceState);

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        initContentView(savedInstanceState);

        initView();/*初始化控件*/
        setToolbar();/*设置toolbar*/
    }

    /**
     * 初始化控件
     */
    private void initView()
    {
        toolbar = (Toolbar) findViewById(R.id.toolbar);
    }

    /**
     * 设置toolbar
     */
    private void setToolbar()
    {
        toolbar.setTitle(title);
        toolbar.setTitleTextColor(Utils.getColor(this, R.color.white));
        setSupportActionBar(toolbar);
        if (getSupportActionBar() != null)
        {
            getSupportActionBar().setHomeButtonEnabled(true);
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        }
    }

    /**
     * toolbar返回键监听
     *
     * @param item
     * @return
     */
    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
        if (item.getItemId() == android.R.id.home)
        {
            finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * 设置标题
     *
     * @param title
     */
    public void setTitle(String title)
    {
        this.title = title;
    }

}
